$(function(){
	$("#province").children().each(function(index){
		if (trim($(this).text()) == trim($("#receiverProvince").val())){
			$("#province")[0].selectedIndex = index ;
			queryProvince2($("#receiverCity").val(),$("#receiverArea").val());
		}
	});	
	$("#province").change(function() {
		var optionsCity = '<option value="">== 选择城市 ==</option>';
		$("#city").html(optionsCity);
		var options = '<option value="">== 选择地区 ==</option>';
		$("#district").html(options);
		queryProvince2('','');
   });	

	$("#city").change(function() {
		queryCity2('');
    });			
});
//去除字串左邊的空白虛格
function ltrim(instr){
	return instr.replace(/^[\s]*/gi,"");
}

//去除字串右邊的空白虛格
function rtrim(instr){
	return instr.replace(/[\s]*$/gi,"");
}

//去除字串前後的空白虛格
function trim(instr){
	instr = ltrim(instr);
	instr = rtrim(instr);
	return instr;
}


//查詢province ajax
function queryProvince2(adCity,adDistrict){
	var province=$("#province").find('option:selected').val();
	$.ajax({
	    url: '/ecmall/shopping/addressdata/findServiceOrderCitys.do',
	    type: 'post',
	    dataType: 'json',
	    data: {id:$("#orderId").val(),province:province},
	    cache: false,
	    success: function( actionResult ){
	    	addressObj(actionResult,'CITY',adCity,adDistrict);
		}
	});
}

//查詢city ajax
function queryCity2(adDistrict){
	var city=$("#city").find('option:selected').val();
	$.ajax({
	    url: '/ecmall/shopping/addressdata/findServiceOrderDistricts.do',
	    type: 'post',
	    dataType: 'json',
	    data: {id:$("#orderId").val(),city:city},
	    cache: false,
	    success: function( actionResult ){
	    	addressObj(actionResult,'DISTRICT','',adDistrict);
		}
	});
}

//Province&City 查詢元件
function addressObj(actionResult,execObj,adCity,adDistrict){
	if(actionResult!=null ){
		var arr_text = actionResult.split("$");		
		if(execObj=='CITY'){								
			var options = '<option value="">== 选择城市 ==</option>';
		}
		if(execObj=='DISTRICT'){
			var options = '<option value="">== 选择地区 ==</option>';
		}
		 for(i=0;i<arr_text.length;i++)
		 {
			 v=arr_text[i].split(",");
			 if(execObj=='CITY'){	
				options += '<option value="' + v[1] + '">' + v[1] + '</option>';
			 }
			if(execObj=='DISTRICT'){
				options += '<option value="' + v[1]+','+v[2] + '">' + v[1] + '</option>';
			}
		 }
		 var aObj;
		 if(execObj=='CITY'){	
			 $("#city").html(options);
			 aObj=$("#city");
		 }
		 if(execObj=='DISTRICT'){
			 $("#district").html(options);
			 aObj=$("#district");
		 }
		 aObj[0].selectedIndex = 0 ; 
		 var tmpObj;
		 if(execObj=='CITY'){	
			tmpObj=adCity;
		 }
		 if(execObj=='DISTRICT'){
			 tmpObj=adDistrict;
		 }
		 if(tmpObj !=""){
			 aObj.children().each(function(index){
				 if(execObj=='CITY'){	
					 if ($(this).text()==adCity){
					       setTimeout(function() {  aObj[0].selectedIndex = index ; 
					       queryCity2(adDistrict);} , 1);
					    }
					 }
					 if(execObj=='DISTRICT'){
						 if ($(this).text().substring(1)==adDistrict || $(this).text()==adDistrict){
						    	setTimeout(function() {  $("#district")[0].selectedIndex = index ;
						    	},1);
						    }
					 }
			 });
		 }
	}
}